function param = prescan2(param, inputstr)
% param is arguments passed in.
% inputstr are cells.
%% determine whether to skip preprocess
skip = 1;
switch lower(param.type)
    case 'hist'
        if isfield(param, 'threshold') && (param.threshold < 1)
            skip = 0;
        end
    case 'point-hist'
        if isfield(param, 'threshold') && (param.threshold < 1)
            skip = 0;
        end
    case 'markov'
        if isfield(param, 'threshold') && (param.threshold < 1)
            skip = 0;
        end
    case 'dlbp'
        if ~isfield(param, 'k_ratio')
            skip = 0;
        end
    case 'dlbp2'
        if ~isfield(param, 'k_ratio')
            skip = 0;
        end
    case 'dlbpk'
        if ~isfield(param, 'mapping')
            skip = 0;
        end
    otherwise
        %pass
end
if skip
    return;
end

%% gather info
fprintf(1, 'Prepare pass: scan files and determine values.\n');

% Defining callback function to prepare values:
%
% _tmp = XXX_prepare_start(param)
%   prepare data for processing
%
% _tmp = XX_prepare_process(img, param, _tmp, count)
%   process images one by one, all data need to pass must be returned
%
% param = XX_prepare_finish(param, _tmp, count)
%   store results into param, it is then returned
%

startfunc = 0;
processfunc = 0;
finishfunc = 0;
switch lower(param.type)
    case 'dlbp'
        startfunc = @DLBP_prepare_start;
        processfunc = @DLBP_prepare_process;
        finishfunc = @DLBP_prepare_finish;
    case 'dlbpk'
        startfunc = @DLBPk_prepare_start;
        processfunc = @DLBPk_prepare_process;
        finishfunc = @DLBPk_prepare_finish;
    case 'hist'
        error('pre_scan not implemented');
        %% TODO: determine threshold
        % scan total histogram of all files
        % returned: 1)count 2)param.threshold
    case 'point-hist'
        error('pre_scan not implemented');
        %% TODO: determine threshold
    case 'markov'
        error('pre_scan not implemented');
        %% TODO: determine threshold
    otherwise
        warning('pre_scan should not be called for type %s', type);
        return;
end

%% start_handle
if isa(startfunc, 'function_handle')
    data = startfunc(param);
else
    data = struct();
end

for z = 1:param.input_size
    count = 0;
    s = inputstr{z};
    disp(s.indir);
    p = randperm(s.count);
    for i = 1:s.count
        if (count >= param.ntrain)
            break;
        end
        name = s.inlist(p(i)).name;
        fullname = [s.indir,'/',name];
        if param.verbose > 1
            disp(name);
        end
        try
            img = imread(fullname);
            if isa(processfunc, 'function_handle')
                data = processfunc(img, param, data, count);
            end
            count = count + 1;
        catch ME
            if param.verbose > 0
                fprintf('%s : FAIL\n', name);
            end
        end
        fclose('all');
    end    
end

if param.verbose > 0
    fprintf(1, '%d files preprocessed.\n', count);
end

%% finish_handle
if isa(finishfunc, 'function_handle')
    param = finishfunc(param, data, count);
end

end
